﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using BeatEmUp.Characters;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;
using BeatEmUp.Characters.Enemies;
using System;
using BeatEmUp.Items.Weapons;
using System.Diagnostics;

namespace BeatEmUp
{
    [Serializable]
    class PlayerWeapon
    {
        Vector2 position;
        Vector2 size;
        Vector2 playerHandPosition;
        FaceDirectionEnum currentDirection;
        float angle;
        int i = 0;
        bool isAttacking;
        KeyboardState keyboard,previousKey;
        GamePadState gamepad, previousGamepad;
        Weapon weapon; 
        Random rand;

        public PlayerWeapon(Vector2 position,Vector2 size,string activeWeapon)
        {
            playerHandPosition = new Vector2(10, 48);
            this.size = size;
            angle = 0.0f;
            isAttacking = false;
            currentDirection = FaceDirectionEnum.Right;
            rand = new Random();
        }

        public void LoadContent(ContentManager content)
        {
            weapon = new Weapon(1, Enums.MonsterType.Normal, "Club of Justice","Club 3", 
                content.Load<Texture2D>("./Sprites/Weapons/Club 3"), 1, Vector2.Zero, Enums.WeaponLocation.OnPlayer);
            this.position = new Vector2(position.X + playerHandPosition.X, position.Y + playerHandPosition.Y-weapon.Height/2);
        }

        public void UpdatePosition(Vector2 position,FaceDirectionEnum direction,EnemiesManager enemies,int attack,Player player)
        {
            keyboard = Keyboard.GetState();
            gamepad = GamePad.GetState(PlayerIndex.One);
            
            this.currentDirection = direction;
            if(isAttacking)i++;
            
            switch (currentDirection)
            {
                case FaceDirectionEnum.Right:
                    playerHandPosition = new Vector2(16, 48);
                    this.position = new Vector2(position.X + playerHandPosition.X, position.Y + playerHandPosition.Y - weapon.Height / 2);
                    if (isAttacking)
                    {
                        if (i == 5)
                        {
                            angle += 30;
                            if (angle >= 60)
                            {
                                isAttacking = false;
                                angle = 0;
                            }
                            i = 0;
                        }   
                    }
                    break;
                case FaceDirectionEnum.Left:
                    if(player.CurrentFrame == 1||player.CurrentFrame ==3)
                        playerHandPosition = new Vector2(11, 52);
                    else
                        playerHandPosition = new Vector2(11, 48);                        
                    this.position = new Vector2(position.X + playerHandPosition.X - weapon.Width, position.Y + playerHandPosition.Y - weapon.Height / 2);
                    if (isAttacking)
                    {
                        if (i == 5)
                        {
                            angle -= 30;
                            if (angle <= -60)
                            {
                                isAttacking = false;
                                angle = 0;
                            }
                            i = 0;
                        }
                    }
                    break;
            }

            if ((keyboard.IsKeyDown(Keys.Space)&&previousKey.IsKeyUp(Keys.Space))||(gamepad.IsButtonDown(Buttons.A)&&previousGamepad.IsButtonUp(Buttons.A)))
            {
                if (currentDirection == FaceDirectionEnum.Right)
                    angle = -30;
                if (currentDirection == FaceDirectionEnum.Left)
                    angle = 30;
                isAttacking = true;
                SoundManager.PlayerSwordUseSound();
            }
            previousKey = keyboard;
            previousGamepad = gamepad;

            if (isAttacking)
            {
                foreach (Enemies enemy in enemies.EnemiesList)
                {
                    if (GetWeaponRectangle().Intersects(enemy.GetPosition()))
                        if (!enemy.IsAttacked)
                        {
                            SoundManager.EnemyInjuredSound();
                            int move = 0;
                            if (currentDirection == FaceDirectionEnum.Right)
                                move = 20;
                            else
                                if (currentDirection == FaceDirectionEnum.Left)
                                    move -=20;
                            enemy.MoveEnemy(move);
                            Debug.WriteLine("Player attack power: " + attack);
                            player.DamageGiven(enemy.Damage(weapon.CalculateDamage())+attack);
                        }
                }
            }
        }

        private Rectangle GetWeaponRectangle()
        {
            if (currentDirection == FaceDirectionEnum.Right)
                return new Rectangle((int)position.X, (int)position.Y, weapon.Width, weapon.Height);
            if (currentDirection == FaceDirectionEnum.Left)
                return new Rectangle((int)position.X, (int)position.Y, weapon.Width, weapon.Height);
            return Rectangle.Empty;
        }

        public void Draw(SpriteBatch sb)
        {
            switch (currentDirection)
            {
                case FaceDirectionEnum.Right:
                    if(isAttacking)sb.Draw(weapon.Texture, GameManager.customResolution.ScaleRectangle(
                        new Rectangle((int)position.X, (int)position.Y, weapon.Width, weapon.Height)), 
                        new Rectangle(0, 0, weapon.Width, weapon.Height), Color.White, MathHelper.ToRadians(angle), 
                        Vector2.Zero, SpriteEffects.None, 0);
                    else
                        sb.Draw(weapon.Texture,  GameManager.customResolution.ScaleRectangle(
                            new Rectangle((int)position.X-4, (int)position.Y, weapon.Width, weapon.Height)), Color.White);
                    break;
                case FaceDirectionEnum.Left:
                    if (isAttacking) sb.Draw(weapon.Texture,GameManager.customResolution.ScaleRectangle(
                        new Rectangle((int)position.X + weapon.Width, (int)position.Y + weapon.Height - 2, weapon.Width, weapon.Height)), 
                        new Rectangle(0, 0, weapon.Width, weapon.Height), Color.White, MathHelper.ToRadians(angle), 
                        new Vector2(weapon.Width, weapon.Height), SpriteEffects.FlipHorizontally, 1);
                    else
                        sb.Draw(weapon.Texture,GameManager.customResolution.ScaleRectangle(
                            new Rectangle((int)position.X, (int)position.Y-4, weapon.Width, weapon.Height)),
                            new Rectangle(0, 0, weapon.Width, weapon.Height),Color.White, 0, Vector2.Zero, SpriteEffects.FlipHorizontally, 0);
                    break;
            }
        }

        public Weapon Weapon
        {
            get
            {
                return weapon;
            }
            set
            {
                weapon = value;
            }
        }
    }
}
